<script>
	import config from '@/app.config.js'
	import util from '@/utils/util.js'
	import vkUtil from './uni_modules/vk-uni-pay/js_sdk/vk-util'
	import checkUpdate from "./uni_modules/uni-upgrade-center-app/utils/check-update"
	var that
	export default {
		methods: {
			//微信网页登录
			mpLogin() {
				//存在openid了 就不再走下面流程
				if (uni.getStorageSync("openid") && uni.vk.callFunctionUtil.checkToken())
					return
				uni.removeStorageSync("openid")

				// 记录登陆前的url 方便后面跳转回去 h5环境下直接用普通跳转方法就可以
				let mpConfig = getApp().globalData.mpWeixinConfig
				if (util.isWechat() && mpConfig.enabled) {
					//参数中不带code 不是回调 拦截 准备登录
					if (!uni.getStorageSync("initUrl")) {
						uni.setStorageSync('initUrl', window.location.href)
					}
					if (window.location.href.includes('login')) {
						uni.setStorageSync('initUrl', window.location.href.split('pages')[0])
					}
					if (window.location.href.indexOf('?code=') == -1 && window.location.href.indexOf('&code=') == -1) {
						window.location.href =
							'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
							mpConfig.appid +
							'&redirect_uri=' +
							window.location.href +
							'&response_type=code&scope=snsapi_base#wechat_redirect';
					} else {
						let code;
						if (window.location.href.indexOf('?code=') !== -1) {
							code = window.location.href.split('?code=')[1].split('&')[0];
						} else {
							code = window.location.href.split('&code=')[1].split('&')[0];
						}
						if (!uni.vk.callFunctionUtil.checkToken()) {
							//处理code 自动登录
							uni.vk.callFunction({
								url: "user/pub/loginByWeixin",
								data: {
									code: code
								},
								success(data) {
									uni.setStorageSync("openid", data.userInfo.wx_openid.h5)
									that.globalData.openid = data.userInfo.wx_openid.h5
									// uni.switchTab({
									// 	url: "/pages/index/homePage"
									// })
									// uni.navigateBack()
									window.location.href = uni.getStorageSync("initUrl")
									uni.removeStorageSync('initUrl')
								}
							})
						} else {
							//存储openid 用于登录
							vkUtil.getH5Openid(code).then((res) => {
								uni.setStorageSync("openid", res.openid)
								that.globalData.openid = res.openid
								// uni.navigateBack()
								window.location.href = uni.getStorageSync("initUrl")
								uni.removeStorageSync('initUrl')
							});
						}
					}
				}
			}
		},
		// 监听 - 页面404
		onPageNotFound: function(e) {
			uni.redirectTo({
				url: config.error.url
			})
		},
		onLaunch: function(e) {
			that = this;
			// util.getAccessToken()
			// util.sendTemplate()
			//是微信网页打开 且 未登录
			uni.vk.callFunctionUtil.updateRequestGlobalParam({
				"platform": {
					regExp: "^client/",
					data: {
						platform: "h5",
						runtime: util.platform(),
						is_ios: util.isIosApp
					}
				}
			});

			//用户中微信心登录时受此影响 需要单独配置
			uni.vk.callFunctionUtil.updateRequestGlobalParam({
				"platform": {
					regExp: "^user/",
					data: {
						platform: util.getWeiXinLoginPlatfo(),
						runtime: util.platform(),
						is_ios: util.isIosApp
					}
				}
			});

			//获取系统配置
			uni.vk.callFunction({
				url: 'client/app/pub/init',
				success(data) {
					getApp().globalData.appConfig = data.data.appConfig
					getApp().globalData.mpWeixinConfig = data.data.mpWeixinConfig
					uni.vk.setVuex('$app.vipRole', data.data.vipRole);
					that.mpLogin()
				}
			});

			//获取个人资料
			if (uni.vk.callFunctionUtil.checkToken()) {
				uni.vk.callFunction({
					url: 'user/kh/getMyUserInfo',
					success(data) {
						getApp().globalData.userInfo = data.userInfo
					}
				});
			}

			// #ifdef APP-PLUS
			//检查版本更新
			checkUpdate()
			//idfa有需要的用户在应用首次启动时自己获取存储到storage中
			//https://ask.dcloud.net.cn/article/36107
			/*if(~plus.storage.getItem('idfa')){
				plus.device.getInfo({//需要勾选IDFA
					success:function(e){  
						console.log('idfa =  '+JSON.stringify(e.idfa));  
					},
					fail:function(e){
						console.log('getDeviceInfo failed: '+JSON.stringify(e));  
					}  
				});
			}*/
			// #endif
			// #ifdef APP-PLUS
			uni.$on("upDataClientId", res => {
				console.log(plus.push.getClientInfo().clientid);
				this.vk.userCenter.updateUser({
					data: {
						clientid: plus.push.getClientInfo().clientid
					},
					success: function(data) {}
				});
			})

			//监听push推送通知
			plus.push.addEventListener('receive', ({
				type,
				title,
				content,
				payload
			}) => {
				// 点赞时我的显示角标
				if (payload.type == 'like' || payload.type == 'talk') {
					uni.showTabBarRedDot({
						index: 3
					})
					// 通知
				}
				//如果type!='receive'是自己本地插件的push消息栏，“拦截”避免死循环'，安卓系统没有这个问题
				if (type == 'receive' || uni.getSystemInfoSync().platform != "ios") {
					plus.push.createMessage(content, JSON.stringify(payload), {
						title: payload.title,
						subtitle: payload.content
					});
				}
			});
			//监听点击通知栏
			plus.push.addEventListener('click', (e) => {
				if (typeof e.payload != 'object') e.payload = JSON.parse(e.payload);
				console.log(e.payload)
				// 点赞信息
				if (e.payload.type == 'like' || e.payload.type == 'talk') {
					uni.switchTab({
						url: '/pages/mine/index'
					})
					return
				}
			});
			// #endif
		},
		globalData: {
			openid: undefined,
			appConfig: {},
			userInfo: {},
			userVipRole: []
		}
	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	/*每个页面公共css */
	@import "uview-ui/index.scss";

	.nav-slot-wrap-title {
		font-size: 44rpx;
		font-weight: 500;
		color: #FFFFFF;
		font-family: PingFangSC-Medium, PingFang SC;
		margin-left: 24rpx;
		letter-spacing: 7rpx;
	}

	.nav-slot-wrap-title-black {
		color: #333333;
	}

	.loadmore {
		width: 100%;
		height: auto;
		padding-top: 15px;
		padding-bottom: 15px;
	}

	/************************************************************
	** 请将全局样式拷贝到项目的全局 CSS 文件或者当前页面的顶部 **
	** 否则页面将无法正常显示                                  **
	************************************************************/

	html {
		font-size: 16px;
	}

	body {
		margin: 0;
		font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans',
			'Droid Sans', 'Helvetica Neue', 'Microsoft Yahei', sans-serif;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
	}

	view,
	image,
	text {
		box-sizing: border-box;
		flex-shrink: 0;
	}

	#app {
		width: 100vw;
		height: 100vh;
	}

	.flex-row {
		display: flex;
		flex-direction: row;
	}

	.flex-col {
		display: flex;
		flex-direction: column;
	}

	.justify-start {
		display: flex;
		justify-content: flex-start;
	}

	.justify-center {
		display: flex;
		justify-content: center;
	}

	.justify-end {
		display: flex;
		justify-content: flex-end;
	}

	.justify-evenly {
		display: flex;
		justify-content: space-evenly;
	}

	.justify-around {
		display: flex;
		justify-content: space-around;
	}

	.justify-between {
		display: flex;
		justify-content: space-between;
	}

	.items-start {
		display: flex;
		align-items: flex-start;
	}

	.items-center {
		display: flex;
		align-items: center;
	}

	.items-end {
		display: flex;
		align-items: flex-end;
	}

	/* #endif */
</style>